<template>
  <div class="section_common">
    <div class="main">
      <el-form ref="form" label-width="100px">

        <el-form-item label="部门">
          <el-cascader
                  size="mini"
                  v-model="dept"
                  :options="depts"
                  :clearable="true"
                  @change="deptChange"
                  :props="{ checkStrictly: true }"
                  filterable
          ></el-cascader>
        </el-form-item>

        <el-form-item label="岗位" required>
          <el-input v-model="posName" size="mini"></el-input>
        </el-form-item>

        <el-form-item label="招聘原因">
          <el-input v-model="reason" type="textarea" :autosize="{ minRows: 2, maxRows: 6}" size="mini"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button v-bind:disabled="isShow" type="primary" size="mini" @click="submitPost"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue';
  import {Toast,Popup,Dialog} from 'vant';
  Vue.use(Popup);

export default {
  data() {
    return {
      nowUser: '',
      title: "岗位申请",
      addApplication: '/mobiles/addRecPosition',
      dept: [],
      depts: [],
      deptId:"",
      posName:"",
      reason:"",

      isShow:false

    };
  },
  created() {
    this.nowUser=JSON.parse(localStorage.getItem('userInfo')).userId;
    this.isPermissionqx();
    this.selDept();
  },
  activated() {
    this.isPermissionqx();
  },

  methods: {
    //只有组长以上权限才可以进行招聘需求的申请，判断当前用户是否有权限
    isPermissionqx(){
      this.$https.post('/mobiles/isPermissionqx', {
        userId:this.nowUser,
        notRname:"组员"
      }).then((res)=>{
        if (res.data.code=='500'){
          //this.code=500;
          //this.$toast(res.data.message);
          Dialog.alert({
            message: res.data.message,
          }).then(() => {
            this.$router.go(-1);
          });
        }

      }).catch(err => {
        console.log(err);
      });
    },

    //初始化选择部门
    selDept(){
      this.$https.post('/mobiles/selPCDept').then((res)=>{
        this.depts=res.data.data;
      }).catch(err => {
        console.log(err);
      });
    },
    //点击获得选中的部门
    deptChange(value){
      //console.log(value)
      this.deptId=value[value.length-1];
    },

    // 返回
    goBack() {
      this.$router.go(-1);
    },
    // 提交
    submitPost() {
      if (!this.isNull()) return false;
      this.$https.post(this.addApplication, {
        nowUser:this.nowUser,
        deptId:this.deptId,
        posName:this.posName,
        reason: this.reason,

      }).then(() => {
        Toast("岗位申请成功");
        this.isShow=true;
        this.$router.go(-1);
      }).catch(err => {
        console.log(err)
      });
    },
    //判断相关数据是否为空
    isNull() {
      if (this.deptId == '') {
        this.$toast("部门不能为空");
        return false;
      }
      if (this.posName=='') {
        this.$toast("岗位不能为空");
        return false;
      }
      return true;
    },

  },
};
</script>

<style lang="scss" scoped>
.main {
  display: flex;
  justify-items: center;
  margin: 0 auto;
  .el-form {
    padding-right: 10px;
    .el-form-item {
      .el-form-item__label {
        line-height: 16px;
      }
      margin-bottom: 5px;
    }
  }
}
</style>